/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.chart.editors;

import java.awt.Color;
import java.awt.Graphics;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.swing.JPanel;

/**
 *
 * @author sammael
 */
public class ColorPanel extends JPanel implements Serializable {
    
    public static final String PROP_COLOR = "colorProperty";
    private String color = "0x337744";
    private String alpha = "FF";
    private PropertyChangeSupport propertySupport;
    
    public ColorPanel() {
        propertySupport = new PropertyChangeSupport(this);
    }
    
    public ColorPanel(Color color) {
        propertySupport = new PropertyChangeSupport(this);
        this.color = "0x"+Integer.toHexString(color.getRGB());
        
    }
    /**
     * @return the Color
     */
    public String getColor() {
        return color;
    }

    /**
     * @param Color color w hex default = 0x337744
     */
    public void setColor(String color) {
        if(!color.startsWith("0x")){
            color = "0x"+color;
        }
        String oldValue = this.color;
        this.color = color;
        repaint();
        propertySupport.firePropertyChange(PROP_COLOR, oldValue, color);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        if(color.length()>8){
            alpha = color.substring(2,4);
            color = "0x" + color.substring(4,10);
        }
        try{
        Color c = Color.decode(color);
        g.setColor(c);
        g.fillRect(0,0, this.getSize().width, this.getSize().height);
        }catch(Exception e){
            g.setColor(Color.WHITE);
            g.fillRect(0,0, this.getSize().width, this.getSize().height);
            g.setColor(Color.red);
            g.drawLine(0, 0, this.getSize().width, this.getSize().height);
            g.drawLine(0,this.getSize().height, this.getSize().width, 0);
        }
    }
}
